package com.zx.ishopping.controller.admin;

import com.zx.ishopping.DTO.CategoryDTO;
import com.zx.ishopping.DTO.CategoryPageQueryDTO;
import com.zx.ishopping.entity.Category;
import com.zx.ishopping.results.PageResult;
import com.zx.ishopping.results.Result;
import com.zx.ishopping.services.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员分类控制器
 * @author 祝鑫
 */

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类信息列表
     * @return
     */
    @GetMapping("/getCategory")
    public Result<List<Category>> getCategoryList(){
        List<Category> list = categoryService.getCategoryList();
        return Result.success(list);
    }

    /**
     * 插入分类信息
     * @param categoryDTO
     */
    @PostMapping("/insertCategory")
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除分类信息
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteCategory(@RequestBody List<Long> ids){
        categoryService.deleteCategory(ids);
        return Result.success();
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }
    
    @GetMapping("/getCategoryById/{id}")
    public Result getCategoryById(@PathVariable Long id){
        log.info("根据id查询分类信息：{}",id);
        Category category = categoryService.getCategoryById(id);
        return Result.success(category);
    }
}
